package com.ftg.learn.system.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.common.dto.LoginDto;
import com.ftg.learn.common.util.R;
import com.ftg.learn.common.vo.LoginVo;
import com.ftg.learn.system.entity.SysUserEntity;
import com.ftg.learn.system.mapper.SysUserMapper;
import com.ftg.learn.system.service.IuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 布衣
 */
@Service
public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements IuserService{

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    private Lock redisLock = new ReentrantLock();
    @Override
    public R<LoginVo> login(LoginDto login) {
        //临界处理
        if (login.getUsername() == null && login.getUsername().isEmpty()) {
            return R.build(401, "用户名不能为空", null);
        }
        if (login.getPassword() == null && login.getPassword().isEmpty()) {
            return R.build(402, "密码不能为空", null);
        }

        SysUserEntity loginVo = this.getBaseMapper().getOneUserByName(login.getUsername());
        if(loginVo == null){
            return R.build(403, "用户不存在", null);
        }
        if(!loginVo.getPassword().equals(login.getPassword())){
            return R.build(404, "密码错误", null);
        }

        LoginVo l1 = new LoginVo();
        l1.setUsername(loginVo.getUsername());
        l1.setPhone(loginVo.getPhone());
        l1.setEmail(loginVo.getEmail());
        l1.setDeptId(loginVo.getDeptId());
        l1.setRealName(loginVo.getRealname());
        return R.build(200, "登陆成功", l1);
    }

    @Override
    public  R<String> listPage(Integer page, Integer pageSize) {
        try{
            redisLock.lock();
            StringBuilder sb = new StringBuilder("user:list.");
            sb.append(page)
                    .append(".")
                    .append(pageSize);
            IPage<SysUserEntity> pageList = new Page<>(page,pageSize);

            String value = redisTemplate.opsForValue().get(sb.toString());
            if (value == null) {
                List<SysUserEntity> list = this.list(pageList);
                pageList.setRecords(list);
                String jsonStr = JSONUtil.toJsonStr(pageList);
                redisTemplate.opsForValue().set(sb.toString(),jsonStr);
            }

            return R.build(200,"查询成功",redisTemplate.opsForValue().get(sb.toString()));
        }catch (Exception e){
            return R.build(500,"查询失败"+e.getMessage(),null);
        }finally {
            redisLock.unlock();
        }
        //1 :  有效时间  随机时间  加动态刷新
        //2 ：redis序列化
    }

}
